package br.com.pheat.servlets;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import br.com.pheat.beans.Refeicao;
import br.com.pheat.beans.RefeicaoAlimento;
import br.com.pheat.beans.Usuario;
import br.com.pheat.dao.RefeicaoDAO;

public class RefeicaoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

    public RefeicaoServlet() {
        super();
    }

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		Usuario usuario = (Usuario) session.getAttribute("UsuarioLogado");
		Integer action = Integer.parseInt(request.getParameter("action"));
		switch(action){
			case 1:
				Refeicao refeicao = new Refeicao();
				refeicao.setTipo_refeicao(request.getParameter("tipoRefeicao"));
				refeicao.setData(request.getParameter("dataRefeicao"));
				refeicao.setRefeicaoAlimentos((List<RefeicaoAlimento>)session.getAttribute("refeicaoAlimentos"));
				try {
					RefeicaoDAO.inserirRefeicao(refeicao, usuario.getCodigo());
					session.removeAttribute("refeicaoAlimentos");
				} catch (SQLException e) {
					e.printStackTrace();
				}
				response.sendRedirect("logado.jsp");
				break;
			case 2:
				try{
				List<Refeicao> refeicoes = RefeicaoDAO.getRefeicao(request.getParameter("data_refeicao"),usuario.getCodigo());
				session.setAttribute("refeicoes", refeicoes);
				}catch(SQLException e){
					e.printStackTrace();
				}
				response.sendRedirect("consultaRefeicao.jsp");
				break;
		}
		
	}

}
